import {
  imageUrl
} from "../utils/imageutil"
Page({

  data: {
    imageUrl,
    list: [],
    page: 1,
    pagesize: 10,
    flag: true
  },

  onLoad(options) {

  },

  onShow() {
    this.setData({
      list: [],
      page: 1,
      flag: true
    })
    this.user_vehiclelist()
  },

  user_vehiclelist() {
    if (!this.data.flag) {
      return
    }
    this.setData({
      flag: false
    })
    getApp().globalData.posts('/index.php/api/app/user/sorting/ouyoung_api/user_vehiclelist',{
      page:this.data.page,
      size:10,
    }).then(res => {
      if (res.data.data.length == 0) {
        this.setData({
          flag: false
        })
      } else {
        let arr = this.data.list.concat(res.data.data);
        this.setData({
          list: arr,
          page: this.data.page + 1,
          flag: true
        })
      }
    })
  },

  onReachBottom() {
    // this.user_vehiclelist()
  },

  user_delvehicle(e) {
    let id = e.currentTarget.dataset.id;
    getApp().globalData.posts('/index.php/api/app/user/sorting/ouyoung_api/user_delvehicle?id=' + id).then(res => {
      getApp().showModal(res.data.msg).then(() => {
        if (res.data.code == 1) {
          this.setData({
            list: [],
            page: 1,
            flag: true
          })
          this.user_vehiclelist()
        }
      })
    })
  },

  to_add(e) {
    let id = e.currentTarget.dataset.id;
    if (id) {
      wx.navigateTo({
        url: './add?id=' + id,
      })
    } else {
      wx.navigateTo({
        url: './add',
      })
    }
  },

  change_car(e) {
    let id = e.currentTarget.dataset.id
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      id
    });
    wx.navigateBack({
      delta: 1
    });
  },


})